<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/12/24
 * Time: 14:27
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Banner extends Base
{
    /**
     * 列表展示
     * @return mixed|\think\response\Json
     */
    public function index()
    {
        if (request()->isAjax()) {
            extract(input());
            $map =array();
            if(isset($province)&&$province!="")
            {
                $map['banner_part'] = $province;
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('banner')
                ->where($map)
                ->count();// 获取总条数
            $lists = Db::name('banner')
                ->page($NowPage, $limits)
                ->where($map)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }


    /***
     * @return mixed|\think\response\Json
     * 添加
     */
    public function add()
    {
        if(Request::instance()->isPost()){
            $banner_title =input('video_modular_name');
            $music_type_img= input('video_modular_img');
            $banner_type = input('param.f_id');
            if($banner_type==1){
                $data['f_id'] = input('param.banner_new');
            }
            if($banner_type==2){
                $data['f_id'] = input('param.banner_new1');
            }
            if($banner_type==3){
                $data['banner_content'] = input('param.new_content');
            }
            $data['banner_title'] = $banner_title;
            $data['banner_thumb'] = $music_type_img;
            $data['banner_part'] = input('param.banner_part');
            $data['banner_type'] = $banner_type;
            $data['banner_time'] = date('Y-m-d H:i:s',time());
            $res = Db::table('up_banner')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $info = Db::table("up_video")->select();
        $info1 = Db::table("up_music")->select();
        $this->assign('info',$info);
        $this->assign('info1',$info1);
        return $this->fetch();
    }

    /***
     * @return mixed|\think\response\Json
     */
    public function update(){
        if(Request::instance()->isPost()){
            $id = input('param.id');
            $banner_title =input('video_modular_name');
            $music_type_img= input('video_modular_img');
            $banner_type = input('param.f_id');
            if($banner_type==1){
                $data['f_id'] = input('param.banner_new');
                $data['banner_content'] = '';
            }
            if($banner_type==2){
                $data['f_id'] = input('param.banner_new1');
                $data['banner_content'] = '';
            }
            if($banner_type==3){
                $data['banner_content'] = input('param.new_content');
                $data['f_id']='';
            }
            if(!empty($music_type_img)){
                $data['banner_thumb'] = $music_type_img;
            }
            $data['banner_title'] = $banner_title;
            $data['banner_part'] = input('param.banner_part');
            $data['banner_type'] = $banner_type;
            $res = Db::table('up_banner')->where('banner_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $id = input('param.id');
        $info = Db::table('up_banner')->where('banner_id',$id)->find();
        $this->assign('id',$id);
        $this->assign('info',$info);

        $info = Db::table("up_video")->select();
        $info1 = Db::table("up_music")->select();
        $this->assign('info2',$info);
        $this->assign('info1',$info1);
        return $this->fetch();
    }

    /***
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $task_id = input('param.id');
        $res = Db::name('banner')->where('banner_id',$task_id)->delete();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }
}